<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\BaseController;
use app\domain\admin\CommonDomain;
use app\service\RedisClusterService;
use think\facade\Db;
use think\response\Json;

class Common extends BaseController
{
    public function __get($name)
    {
        return $this->$name ?? $this->request->$name ?? $this->request[$name] ?? request()->param($name) ?? null;
    }

    public function test()
    {
        try {
            //rabbitmqPush(json_encode(['test' => 'aaaa'], 320));
            //$res = Db::name('device_data')->getFields();
            //var_export(array_column($res, 'name'));
            //dd($res);
            $redis = RedisClusterService::run();
            //dd($redis);
            $redis->set('name', 'zhangsan');
            $data = $redis->get('name');
            exit(var_dump($data));
        } catch (\RedisClusterException $e) {
            exit($e->getMessage());
        }
    }

    public function getCaptcha(): Json
    {
        try {
            $captcha = generateCaptchaBase64();
            return jsonReturn(true, '', ['img_base64' => "data:image/jpeg;base64,{$captcha}"]);
        } catch (\Exception $e) {
            return jsonReturn(false, $e->getMessage());
        }
    }

    public function admin_menus(CommonDomain $domain): Json
    {
        try {
            $type = request()->param('type', 'all');
            $result = $domain->admin_menus($type);
            return jsonReturn(true, lang($domain->errMsg), $result, $domain->statusCode);
        } catch (\Exception $e) {
            return jsonReturn(false, $e->getMessage());
        }
    }

    public function admin_group(CommonDomain $domain): Json
    {
        try {
            $result = $domain->admin_group();
            return jsonReturn(true, lang($domain->errMsg), $result, $domain->statusCode);
        } catch (\Exception $e) {
            return jsonReturn(false, $e->getMessage());
        }
    }

    public function user_search(CommonDomain $domain): Json
    {
        try {
            $keyword = request()->param('keyword', '');
            $result = $domain->user_search($keyword);
            return jsonReturn(true, lang($domain->errMsg), $result, $domain->statusCode);
        } catch (\Exception $e) {
            return jsonReturn(false, $e->getMessage());
        }
    }

    public function getPermCode(): Json
    {
        try {
            return jsonReturn(true, '', []);
        } catch (\Exception $e) {
            return jsonReturn(false, $e->getMessage());
        }
    }

    public function getMenuList(CommonDomain $domain): Json
    {
        try {
            $result = $domain->getMenuList();
            return jsonReturn(true, $domain->errMsg, $result);
        } catch (\Exception $e) {
            return jsonReturn(false, $e->getMessage());
        }
    }
}
